package com.trinhrae.shooting.moduleAuth.controller;

import com.alibaba.fastjson.JSONObject;
import com.trinhrae.shooting.entity.BaseResponse;
import com.trinhrae.shooting.moduleAuth.service.RegisterVerCodeService;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.io.IOException;

/**
 * @Author: TrinhRae
 * @Package: com.trinhrae.shooting.moduleAuth.controller
 * @Project: shooting
 * @Date: 2025/9/3 15:29
 */
@RestController
@RequestMapping("/api")
public class RegisterController {
    private final RegisterVerCodeService registerVerCodeService;

    public RegisterController(RegisterVerCodeService registerVerCodeService) {
        this.registerVerCodeService = registerVerCodeService;
    }

    /**
     * 发送验证码
     *
     * request:{"email":"xxx@xx.com"}
     * @param request
     * @return
     * @throws MessagingException
     * @throws TemplateException
     * @throws IOException
     */
    @PostMapping("/send-code")
    public BaseResponse sendCode(@RequestBody JSONObject request) throws MessagingException, TemplateException, IOException {
        return registerVerCodeService.sendCodeToEmail(request.getString("email"));
    }

    /**
     * 校验验证码
     *
     * request:{"account":"xxx", "verCode":"xxxx"}
     * @param request
     * @return
     */
    @PostMapping("/verify-code")
    public BaseResponse verifyCode(@RequestBody JSONObject request) {
        return registerVerCodeService.checkCode(request);
    }
}
